package com.Questions.DP;


/**
 * 粉刷房子2
 */
public class leetcode265 {
    public static final int maxNum = 20000;
    public int minCostII(int[][] costs) {
        // cost[i][j]表示 将i房子粉刷成j颜色 需要多少成本
        int m = costs.length;
        int n = costs[0].length;
        int[][] dp = new int[m][n];
        for(int i = 0; i < n; ++i) {// 初始条件
            dp[0][i] = costs[0][i];
        }
        for (int i = 1; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                int min = maxNum;
                for (int k = 0; k < n; ++k) {
                    if (k == j) {
                        continue;
                    }
                    min = Math.min(dp[i-1][k], min);
                }
                dp[i][j] = min + costs[i][j];
            }
        }
        int ans = maxNum;
        for (int i = 0; i < n; i++) {
            ans = Math.min(ans, dp[m-1][i]);
        }
        return ans;
    }
}
